package org.aplikator.client.local.command;

import org.aplikator.client.local.Aplikator;
import org.aplikator.client.local.widgets.LoadingLabel;
import org.aplikator.client.local.widgets.TableFactory;
import org.aplikator.client.local.widgets.TableInterface;
import org.aplikator.client.shared.descriptor.ViewDTO;
import org.aplikator.server.persistence.search.Search;
import org.jboss.errai.common.client.api.RemoteCallback;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Widget;

public class ListEntities extends AplikatorActivity {
	TableInterface table;

    public ListEntities(String viewId) {
        super(viewId);
    }

    protected ViewDTO view;
    protected String searchArgument;
    protected HandlerRegistration submitHandlerRegistration;
    //@Inject
    //private Caller<AplikatorService> aplikatorService;

    @Override
    public void onStop() {
    	if (submitHandlerRegistration!=null) {
    		submitHandlerRegistration.removeHandler();
    	}
    }
    
    @Override
    public void start(final AcceptsOneWidget panel, EventBus eventBus) {
        panel.setWidget(new LoadingLabel());
        if (view == null) {
            Aplikator.aplikatorService.call( new RemoteCallback<ViewDTO>() {
                @Override
                public void callback(ViewDTO response) {
                    ListEntities.this.view = response;
                    drawPanel(panel);
                }
            }).getView(getToken());
        } else {
            drawPanel(panel);
        }
    }

    private void drawPanel(AcceptsOneWidget panel) {
    	boolean isSearchEnabled = Boolean.parseBoolean( Aplikator.application.getConfigString(Search.SEARCH_ENABLED));
    	 if (view.getEntity().isIndexed()&&isSearchEnabled) {
         	KeyDownHandler keyDownHandler = new KeyDownHandler() {
 			
         		@Override
         		public void onKeyDown(KeyDownEvent event) {
         			event.stopPropagation();
         			if (event.getNativeKeyCode()==KeyCodes.KEY_ENTER) {
         				String  input = Aplikator.getSearch().getValue();
         				if (input.trim().length()>0) {
         					table.reload(input);
         				} else {
         					table.reload();
         				}
         			}
         		}
         	};
         	Aplikator.getSearch().setEnabled(true);
         	submitHandlerRegistration = Aplikator.getSearch().addKeyDownHandler(keyDownHandler);    		
         } else {
         	Aplikator.getSearch().setEnabled(false);
         }
        if (widget == null) {
            widget = createTable();
        }
        panel.setWidget(widget);
    }

    private Widget widget = null;
    private Widget createTable() {

        table =  TableFactory.create(view.clone(), null, null);
        table.setTitleCallback(new MainPanelTitleCallback() {

            @Override
            public void setTitle(String params) {
                //TODO how to get readable name from action  mainPanel.setTitle(tabName + " (" + params + ")");

            }
        });
        if (searchArgument!=null) {
        	table.reload(searchArgument);
        } else {
        	Aplikator.getSearch().setValue("");
        	table.reload();
        }
        return table.asWidget();
    }



    /*
     * private Widget createDetail(){ final Label label = new Label("Detail");
     * ScrollPanel wrapper = new ScrollPanel(label);
     * //dynamicTreeWrapper.setSize("500px", "300px"); return wrapper; }
     */

}
